package c.x.jy.wexin.controller;

import c.x.jy.common.annotation.Login;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.R;
import c.x.jy.dao.entity.ClubEntity;
import c.x.jy.dao.entity.ClubEvaluationEntity;
import c.x.jy.service.service.ClubEvaluationService;
import c.x.jy.service.service.ClubService;
import c.x.jy.service.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 
 *
 * @author amwhyh
 * @email xxx@xx.com
 * @date 2021-07-12 10:32:15
 */
@RestController
@RequestMapping("wechat/clubevaluation")
public class ClubEvaluationController {
    @Autowired
    private ClubEvaluationService clubEvaluationService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private ClubService clubService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){

        PageUtils page = clubEvaluationService.queryPage(params);

        return R.put(page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		ClubEvaluationEntity clubEvaluation = clubEvaluationService.getById(id);

        return R.put(clubEvaluation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @Transactional
    @Login
    public R save(@RequestBody ClubEvaluationEntity clubEvaluation){
        clubEvaluation.setMemberId(memberService.getCurrUser().getId());
		clubEvaluationService.save(clubEvaluation);
        ClubEntity clubEntity = clubService.getById(clubEvaluation.getClubId());
        if(clubEntity!=null){
            clubEntity.setEvaluationTimes(clubEntity.getEvaluationTimes()+1);
            clubService.updateById(clubEntity);
        }else{
            return R.error("错误的动态");
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")

    public R update(@RequestBody ClubEvaluationEntity clubEvaluation){
		clubEvaluationService.updateById(clubEvaluation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")

    public R delete(@PathVariable("id") Long id){
		clubEvaluationService.removeByIds(Arrays.asList(id));

        return R.ok();
    }

}

